import { mapState } from 'vuex'
import { 
  getWorkDaysInMonth,
  getWorkDaysInYear,
  getWorkDaysBetweenDates 
} from '../utils/timeCalculator'
import workTime from './workTime'

export default {
  mixins: [workTime],

  data() {
    return {
      todayIncome: '0.00',
      monthIncome: '0.00',
      yearIncome: '0.00',
      totalIncome: '0.00',
      workDays: 0,
      hourlyRate: '0.00',
      currentTime: null
    }
  },

  computed: {
    monthEstimate() {
      if (this.workSettings.salary.type === 'monthly') {
        return this.formatAmount(this.workSettings.salary.amount)
      }
      
      const now = new Date()
      const remainingWorkDays = getWorkDaysInMonth(
        now.getFullYear(),
        now.getMonth(),
        this.workSettings.workdays
      )
      const dailyIncome = parseFloat(this.hourlyRate) * this.getWorkHours()
      const total = parseFloat(this.monthIncome) + (dailyIncome * remainingWorkDays)
      return this.formatAmount(total)
    },

    yearEstimate() {
      if (this.workSettings.salary.type === 'monthly') {
        return this.formatAmount(this.workSettings.salary.amount * 12)
      }
      
      const now = new Date()
      const remainingWorkDays = getWorkDaysInYear(
        now.getFullYear(),
        this.workSettings.workdays
      )
      const dailyIncome = parseFloat(this.hourlyRate) * this.getWorkHours()
      const total = parseFloat(this.yearIncome) + (dailyIncome * remainingWorkDays)
      return this.formatAmount(total)
    }
  },

  methods: {
    calculateHourlyRate() {
      if (!this.workSettings || !this.workSettings.salary) return
      
      if (this.workSettings.salary.type === 'monthly') {
        const now = new Date()
        const monthWorkDays = getWorkDaysInMonth(
          now.getFullYear(),
          now.getMonth(),
          this.workSettings.workdays
        )
        const workHours = this.getWorkHours()
        if (monthWorkDays > 0 && workHours > 0) {
          this.hourlyRate = (this.workSettings.salary.amount / monthWorkDays / workHours).toFixed(2)
        } else {
          this.hourlyRate = '0.00'
        }
      } else {
        this.hourlyRate = this.workSettings.salary.amount.toFixed(2)
      }
    },

    calculateIncome(now) {
      if (!this.isWorkday || !this.isWorking) {
        this.todayIncome = '0.00'
        return
      }
      
      const startTime = new Date(now)
      const currentPeriod = this.getCurrentWorkPeriod()
      if (currentPeriod === 'morning') {
        const [hours, minutes] = this.workSettings.morningStart.split(':')
        startTime.setHours(parseInt(hours), parseInt(minutes), 0, 0)
      } else {
        const [hours, minutes] = this.workSettings.afternoonStart.split(':')
        startTime.setHours(parseInt(hours), parseInt(minutes), 0, 0)
      }
      
      const workMs = now.getTime() - startTime.getTime()
      const workHours = workMs / (1000 * 60 * 60)
      
      this.todayIncome = (workHours * parseFloat(this.hourlyRate)).toFixed(2)
    },

    updateIncomeStats(includeToday = true) {
      const now = new Date()
      const dailyIncome = parseFloat(this.hourlyRate) * this.getWorkHours()
      
      // 更新总收入
      const joinDate = new Date(this.workSettings.joinDate)
      if (joinDate > now) {
        joinDate.setTime(now.getTime())
      }
      const totalWorkDays = getWorkDaysBetweenDates(
        joinDate,
        new Date(now.setHours(0, 0, 0, 0)),
        this.workSettings.workdays
      )
      let totalIncome = dailyIncome * totalWorkDays
      
      // 如果是工作日且需要包含今天的收入
      if (includeToday && this.isWorkday && this.isWorking) {
        const startTime = new Date(now)
        const currentPeriod = this.getCurrentWorkPeriod()
        if (currentPeriod === 'morning') {
          const [hours, minutes] = this.workSettings.morningStart.split(':')
          startTime.setHours(parseInt(hours), parseInt(minutes), 0, 0)
        } else {
          const [hours, minutes] = this.workSettings.afternoonStart.split(':')
          startTime.setHours(parseInt(hours), parseInt(minutes), 0, 0)
        }
        const workMs = now.getTime() - startTime.getTime()
        const workHours = workMs / (1000 * 60 * 60)
        totalIncome += workHours * parseFloat(this.hourlyRate)
      }
      this.totalIncome = totalIncome.toFixed(2)
    },

    formatAmount(amount) {
      const num = parseFloat(amount)
      if (isNaN(num)) return '0.00'
      if (num >= 10000) {
        const wan = (num / 10000).toFixed(2)
        return wan + '万'
      }
      return num.toFixed(2).replace(/\B(?=(\d{3})+(?!\d))/g, ',')
    }
  }
} 